import { Component } from 'react'
import Taro from '@tarojs/taro'
import { request } from '@/utils'
import 'taro-ui/dist/style/index.scss'
import './app.less'

class App extends Component {

 async componentDidMount () {
    let user
    try {
      
      const t = await Taro.getStorage({ key: 'userInfo'})
      user = JSON.parse(t.data)
    } catch(e) {
      user = null
    }
    if(user) {
      const { token } = user
      const res = await request({url: 'hkshihua/user/getAllprofile', header: {token}}) 
      if(res.error) return
      if(res.authUser.role === 'OPERATOR') {
        
        Taro.switchTab({url: '/pages/scanQRCode/index'})
      } else {
        Taro.switchTab({url: '/pages/index/index'})
      }  
    }
  }

   componentDidShow () {
    Taro.login({
      success: async (res) => {
        const data = await request({ url: 'hkshihua/user/openId', data: {code: res.code}})
        Taro.setStorage({key: 'openId', data: data.openId})
      }
    })
  }

  componentDidHide () {}

  componentDidCatchError () {}

  // this.props.children 是将要会渲染的页面
  render () {
    return this.props.children 
  }
}

export default App
